/*
  Copyright (c) 2010 Les Newell. All rights reserved

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
To configure use the following:
#define GPS_PORT Serial2
#define GPS_BAUD 38500
//Update rate in samples/second
#define GPS_RATE 10
//Any initialization strings that need to be sent. Note you need to supply your own
//line end characters (if needed)
static const char gpsInit[]="$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n";
#include "gpsNMEA.h"

*/

#include "gps.h"
#include "tinygps.h"

//define the module.
DEFINE_MODULE(GPSNMEA,GPS)
//Note the lack of an opening curly bracket.


    //Only use this to expose sockets and parameters. Do most of your
    //initialisastion in Init()
    GPSNMEA()
    {
    }

    //A parameter has changed. This happens at bootup if the EEPROM
    //contains valid data and at run-time if the user changes a
    //parameter over the serial link
    virtual void ParamChanged(Socket * param)
    {
    }

    //Initialise the module
    //set up hardware etc
    virtual void Init()
    {
        GPS_PORT.begin(GPS_BAUD);
        //we need to poll at this rate to guarantee the rx buffer never overruns
        int loopRate = 500/((GPS_BAUD / 9)/ 128);
        SetInterval(loopRate);
        m_timeout = (4000 / GPS_RATE) / loopRate; //Miss up to 4 samples before we signal an error
        m_countDown = 1;
        SetPriority (10);
    }

    //Here is where you do your work. It is called at the rate defined by SetInterval
    virtual void Loop(const unsigned long& interval)
    {
        if(m_countDown)
        {
            m_countDown --;
            if(!m_countDown)
            {
                m_ok = false;
            }
        }
        return;
        while(GPS_PORT.available())
        {
            GPS_PORT.read();
            if(false && m_gps.encode(GPS_PORT.read()))
            {
                m_gps.f_get_position(m_latitude.GetPointer(),m_longitude.GetPointer());
                m_altitude = m_gps.f_altitude();
                m_heading = m_gps.f_course();
                m_speed = m_gps.f_speed_mps();
                m_ok = true;
            }
        }
    }

private:
    TinyGPS m_gps;
    int m_countDown;
    int m_timeout;

};

GPSNMEA g_GPSNMEA;


